package org.idea.qiyu.framework.gateway.starter.registry;

import java.util.Objects;

/**
 * @Author linhao
 * @Date created in 8:55 上午 2022/4/16
 */
public class URL {

    private String applicationName;

    private String address;

    private Integer port;

    private Long registryTime;

    private Integer status;

    private Integer weight;

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getApplicationName() {
        return applicationName;
    }

    public void setApplicationName(String applicationName) {
        this.applicationName = applicationName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public Long getRegistryTime() {
        return registryTime;
    }

    public void setRegistryTime(Long registryTime) {
        this.registryTime = registryTime;
    }

    public String buildURLStr() {
        String urlStr = this.getAddress() + ":" + this.getApplicationName() + ":" + this.getPort() + ":" + this.getStatus() + ":" + this.getRegistryTime() + ":" + this.getWeight();
        return urlStr;
    }

    public static URL buildURL(String url) {
        if (url == null || url == "") {
            return null;
        }
        String[] urlArr = url.split(":");
        URL result = new URL();
        result.setAddress(urlArr[0]);
        result.setApplicationName(urlArr[1]);
        result.setPort(Integer.valueOf(urlArr[2]));
        result.setStatus(Integer.valueOf(urlArr[3]));
        result.setRegistryTime(Long.valueOf(urlArr[4]));
        result.setWeight(Integer.valueOf(urlArr[5]));
        return result;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        URL url = (URL) o;
        return url.getApplicationName().equals(this.applicationName)
                && url.getRegistryTime().equals(this.registryTime)
                && url.getStatus().equals(this.status)
                && url.getPort().equals(this.port)
                && url.getAddress().equals(this.address);
    }

    @Override
    public int hashCode() {
        return Objects.hash(applicationName, address, port, registryTime, status);
    }

}
